package com.iplatform.commons.utils;

import java.util.Map;

import com.iplatform.module.auth.AuthUserDetailsFacade;
import com.iplatform.module.privilege.PriManagerBean;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import com.alibaba.fastjson.JSONObject;

public class UserUtil {

    /**
     * 获取登陆的 LoginAppUser
     *
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static PriManagerBean getLoginAppUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication oAuth2Auth = (OAuth2Authentication) authentication;
            authentication = oAuth2Auth.getUserAuthentication();
            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) authentication;
                Object principal = authentication.getPrincipal();
                if (principal instanceof PriManagerBean) {
                    return (PriManagerBean) principal;
                }
                Map map = (Map) authenticationToken.getDetails();
//				map = (Map) map.get("principal");
                return JSONObject.parseObject(JSONObject.toJSONString(map), PriManagerBean.class);
            }
        }

        return null;
    }


    /**
     * 获取登陆的后存储的UserDetails对象
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static UserDetails getUserDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication oAuth2Auth = (OAuth2Authentication) authentication;
            authentication = oAuth2Auth.getUserAuthentication();
            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                Object principal = authentication.getPrincipal();
                if (principal instanceof UserDetails) {
                    return  (UserDetails) principal;
                }
            }
        }
        return null;
    }

    /**
     * 获取实际的userDetails对象，
     * 若存储的userDetails是tClass的实例，直接返回
     * 否则判断存储的userDetails是否为AuthUserDetailsFacade实例，若是则返回真实的userDetails实例
     * @param tClass userDetails对象实际class对象
     */
    public static <T extends UserDetails> T getUserDetails(Class<T> tClass) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication oAuth2Auth = (OAuth2Authentication) authentication;
            authentication = oAuth2Auth.getUserAuthentication();
            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                Object principal = authentication.getPrincipal();
                if (principal.getClass().equals(tClass)) {
                    return (T) principal;
                } else if (principal instanceof AuthUserDetailsFacade) {
                    return ((AuthUserDetailsFacade) principal).getUserDetails(tClass);
                }
                return JSONObject.parseObject(JSONObject.toJSONString(authentication.getDetails()),tClass);
            }
        }
        return null;
    }
}
